package com.study.dao.impl;


import com.study.dao.AdminInfoDAO;
import com.study.pojo.AdminInfo;
import com.study.service.AdminService;
import com.study.util.JdbcUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @PackgeName: com.study.dao.impl
 * @ClassName: AdminInfoDAOImpl
 * @Author: 严志祥
 * Date: 2022-05-04 16:15
 * project name: web_project_medal
 * @Version:
 * @Description:
 */
public class AdminInfoDAOImpl implements AdminInfoDAO {
	private final static Logger log = LoggerFactory.getLogger(AdminInfoDAOImpl.class);
    //1.
    Connection conn = JdbcUtil.getConn();

    @Override
    public int insert(AdminInfo entity) throws SQLException {
        //2.
        String sql = "insert into admin_info (admin_name,admin_password,admin_time) values (?,?,?)";
        //3.
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //4.
        pstmt.setObject(1, entity.getAdminName());
        pstmt.setObject(2, entity.getAdminPassword());
        pstmt.setObject(3, entity.getAdminTime());
        //日志输出
        log.debug(sql);
        //5.
        return pstmt.executeUpdate();
    }

    @Override
    public int updateById(AdminInfo entity) throws SQLException {
        String sql = "update admin_info set admin_name = ?,admin_password = ?,admin_time = ? where admin_id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, entity.getAdminName());
        pstmt.setObject(2, entity.getAdminPassword());
        pstmt.setObject(3, entity.getAdminTime());
        pstmt.setObject(4, entity.getAdminId());
      //日志输出
        log.debug(sql);
        return pstmt.executeUpdate();
    }

    @Override
    public int deleteById(Serializable id) throws SQLException {
        String sql = "delete from admin_info where admin_id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, id);
      //日志输出
        log.debug(sql);
        return pstmt.executeUpdate();
    }

    @Override
    public AdminInfo selectById(Serializable id) throws SQLException {
        String sql = "select * from admin_info where admin_id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, id);
        ResultSet rs = pstmt.executeQuery();
        AdminInfo adminInfo = new AdminInfo();
        if (rs.next()) {
            adminInfo.setAdminId(rs.getInt("admin_id"));
            adminInfo.setAdminName(rs.getString("admin_name"));
            adminInfo.setAdminPassword(rs.getString("admin_password"));
            adminInfo.setAdminTime(rs.getDate("admin_time"));
        }
      //日志输出
        log.debug(sql);
        return adminInfo;
    }

    @Override
    public List<AdminInfo> select(int index, int len) throws SQLException {
        String sql = "select * from admin_info order by admin_id desc limit ?,?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, index);
        pstmt.setObject(2, len);
        ResultSet rs = pstmt.executeQuery();
        List<AdminInfo> list = new ArrayList<>();
        while (rs.next()) {
            AdminInfo adminInfo = new AdminInfo();
            adminInfo.setAdminId(rs.getInt("admin_id"));
            adminInfo.setAdminName(rs.getString("admin_name"));
            adminInfo.setAdminPassword(rs.getString("admin_password"));
            adminInfo.setAdminTime(rs.getDate("admin_time"));
            list.add(adminInfo);
        }
      //日志输出
        log.debug(sql);
        return list;
    }

    @Override
    public long count() throws SQLException {
        String sql = "select count(*) from admin_info";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        rs.next();
        int rsInt = rs.getInt("count(*)");
      //日志输出
        log.debug(sql);
        return rsInt;
    }

    @Override
    public AdminInfo selectByName(String username) throws SQLException {
        String sql = "select * from admin_info where admin_name = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1,username);
        ResultSet rs = pstmt.executeQuery();
        AdminInfo adminInfo = new AdminInfo();
        while(rs.next()){
            adminInfo.setAdminId(rs.getInt("admin_id"));
            adminInfo.setAdminName(rs.getString("admin_name"));
            adminInfo.setAdminPassword(rs.getString("admin_password"));
            adminInfo.setAdminTime(rs.getDate("admin_time"));
        }
      //日志输出
        log.debug(sql);
        return adminInfo;
    }

    @Override
    public boolean selectByAdminName(String adminName) throws SQLException {
        String sql = "select * from admin_info where admin_name = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1,adminName);
        ResultSet rs = pstmt.executeQuery();
      //日志输出
        log.debug(sql);
        if(rs.next()){
            return true;
        }else{
            return false;
        }
    }
}
